<?php

namespace Common\Controller;

use Think\Controller;

use Think\Page;

class BaseManageController extends Controller
{
    protected $_admin = array();
    protected $_CONFIG = array();

    public function __construct()
    {
        parent::__construct();
        $this->_admin = session('admin');

        if (strtolower(CONTROLLER_NAME) != 'login' && strtolower(CONTROLLER_NAME) != 'public') { //public 不受权限控制
            if (empty($this->_admin)) {
                header("Location: " . U('login/index'));
                exit();
            }
            if ($this->_admin['role_id'] != 1) {

                $this->_admin['menu_list'] = D('RoleMaps')->getMenuIdsByRoleId($this->_admin['role_id']);

                if (strtolower(CONTROLLER_NAME) != 'index') { //其他页面需要判断权限
                    $menu_action = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
                    $menu = D('Menu')->fetchAll();
                    $menu_id = 0;
                    foreach ($menu as $k => $v) {
                        if ($v['menu_action'] == $menu_action) {
                            $menu_id = (int) $k;
                            break;
                        }
                    }
                    if (empty($menu_id) || !isset($this->_admin['menu_list'][$menu_id])) {
                        $this->error('很抱歉您没有权限操作模块:' . $menu[$menu_id]['name']);
                    }
                }
            }
        }
        $this->assign('admin', $this->_admin);
    }

    protected function simpleSuccess($message, $jumpUrl = '', $time = 3000)
    {
        $str = '<script>';
        $str .='parent.success("' . $message . '",' . $time . ',\'jumpUrl("' . $jumpUrl . '")\');';
        $str.='</script>';
        exit($str);
    }


    protected function simpleError($message, $time = 3000, $yzm = false)
    {
        $str = '<script>';
        if ($yzm) {
            $str .='parent.error("' . $message . '",' . $time . ', "verifyCode()");';
        } else {
            $str .='parent.error("' . $message . '",' . $time . ');';
        }
        $str.='</script>';
        exit($str);
    }

    /**
     * 列表
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function index($Model = CONTROLLER_NAME)
    {
        $Model = D($Model);
        $data = $this->lists($Model);
        $this->assign('list', $data);
        $this->display();
    }

    /**
     * 分页
     * @author Kevin_ren  <330202207@qq.com>
     */
    protected function lists($model, $where = array(), $order = '', $base = array('is_del' => array('eq', 0)))
    {

        $options = array();
        if (is_string($model)) {
            $model = M($model);
        }

        $pk = $model->getPk();

        if (empty($order)) {
            $order = $pk . ' desc';
        }

        $options['where'] = array_filter(array_merge($base, $where), function ($val) {
            if ($val === '' || $val === null) {
                return false;
            } else {
                return true;
            }
        });

        if (empty($where)) {
            $options['where'] =  $base;
        }

        $total = $model->where($options['where'])->count();

        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;

        $page = new \Think\Page($total, $listRows, $options);

        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }

        $show = $page->show();
        $this->assign('page', $show);
        $this->assign('_total', $total);
        $limit = $page->firstRow . ',' . $page->listRows;

        return $model->where($options['where'])->order($order)->limit($limit)->select();
    }


    /**
     * 添加
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function add()
    {
        $this->assign('data', null);
        $this->display('save');
    }

    /**
     * 编辑
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function edit($Model = CONTROLLER_NAME)
    {
        $Model = D($Model);
        $id    = I('id');
        $data  = $Model->fetchAll();
        $this->assign("data", $data[$id]);
        $this->display('save');
    }

    /**
     * 保存
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function save($Model = CONTROLLER_NAME)
    {
        $Model = D($Model);
        $data  = $Model->saveData();
        if ($data !== false) {
            $Model->cleanCache();
            $this->simpleSuccess("保存成功", U("index"));
        } else {
            $this->simpleError($Model->getError());
        }
    }

    /**
     * 删除
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function delete($Model = CONTROLLER_NAME)
    {
        $Model = D($Model);
        $id = I('id');
        $Model->save(array('id' => $id, 'is_del' => 1));
        $Model->cleanCache();
        $this->simpleSuccess('删除成功！', U('index'));
    }
}